/*
 * Copyright 2010 Gal Dolber.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.unnison.demo.client.complexplace;

import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.HasValue;
import com.google.inject.Inject;

import com.unnison.demo.client.complexplace.ComplexPlacePresenter.ComplexPlaceBinder;
import com.unnison.demo.client.main.MainPresenter;
import com.unnison.demo.client.styler.Styler;
import com.unnison.framework.client.GuitPresenter;
import com.unnison.framework.client.View;
import com.unnison.framework.client.binder.GuitBinder;
import com.unnison.framework.client.binder.ViewField;
import com.unnison.framework.client.binder.ViewFields;
import com.unnison.framework.client.binder.ViewHandler;
import com.unnison.framework.client.binder.contributor.RunAsync;
import com.unnison.framework.client.binder.contributor.UserAction;
import com.unnison.framework.client.place.PlaceName;
import com.unnison.framework.client.place.StayPlace;

@RunAsync
@PlaceName("complex")
public class ComplexPlacePresenter extends GuitPresenter<ComplexPlaceBinder> implements StayPlace<ComplexPlaceData> {

    public interface ComplexPlaceBinder extends GuitBinder<ComplexPlacePresenter> {
    }

    private final MainPresenter hasContent;
    ComplexPlaceData data;

    @ViewField
    HasText textbox;

    @ViewField
    HasValue<Boolean> checkbox;

    @ViewField
    Styler color;

    @ViewField
    Styler fontSize;

    @Inject
    public ComplexPlacePresenter(MainPresenter hasContent) {
        this.hasContent = hasContent;
    }

    @ViewHandler(ValueChangeEvent.class)
    @ViewFields({"checkbox", "textbox"})
    @UserAction("Checkbox or textbox change")
    void checkboxOrTextboxChange(Object source) {
        if (source.equals(checkbox)) {
            data.setEnabled(checkbox.getValue());
        } else if (source.equals(textbox)) {
            data.setText(textbox.getText());
        } else {
            throw new IllegalStateException();
        }
        registerChange();
    }

    @ViewHandler
    void color$click() {
        if (color.getBackgroundColor().equals("red")) {
            data.setColor("blue");
        } else {
            data.setColor("red");
        }
        registerChange();
    }

    @Override
    public void go(ComplexPlaceData data) {
        if (data == null) {
            data = new ComplexPlaceData();
            data.setColor("red");
            data.setEnabled(true);
            data.setText("Hello");
            data.setTextSize(23);
            placeManager.newItem(ComplexPlacePresenter.class, data);
        }

        View view = getView();
        if (data.getColor().equals("blue")) {
            hasContent.setContent(view, 2);
        } else {
            hasContent.setContent(view, 3);
        }

        this.data = data;
        render();
    }

    @Override
    public void leave() {
        //        alerter.alert("Leaving ComplexPlace..."); // We can show an alert, but its annoyng
    }

    @Override
    public String mayLeave() {
        return "Sure you wanna leave?";
    }

    private void registerChange() {
        placeManager.newItem(ComplexPlacePresenter.class, data);
        render();
    }

    private void render() {
        textbox.setText(data.getText());
        checkbox.setValue(data.isEnabled());
        color.setBackgroundColor(data.getColor());
        fontSize.setFontSizePx(data.getTextSize());
    }

    @Override
    public void stay() {
        //        alerter.alert("Staying in ComplexPlace..."); // We can show an alert, but its annoyng
    }
}
